import { service } from './service'

import { config } from './config'

const { default_headers } = config
type requestOptions = {
  url: string;
  method?: string;
  params?: string;
  data?: string|Object|ArrayBuffer;
  responseType?: string;
  headersType?:string;
  default_headers?:string;
}
const request = (option: any) => {
  const { url, method, params, data, headersType, responseType } = option
  return service({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    headers: {
      'Content-Type': headersType || default_headers
    }
  })
}
export default {
  getApi: <T = any>(option: requestOptions) => {
    return request({ method: 'get', ...option }) as unknown as T
  },
  postApi: <T = any>(option: requestOptions) => {
    return request({ method: 'post', ...option }) as unknown as T
  },
  deleteApi: <T = any>(option: requestOptions) => {
    return request({ method: 'delete', ...option }) as unknown as T
  },
  putApi: <T = any>(option: requestOptions) => {
    return request({ method: 'put', ...option }) as unknown as T
  }
}
